#include <stdlib.h>
#include <string>
#include "NapiHelper.h"
#include "Js_GameEngineHelper.h"

napi_env Js_GameEngineHelper::_env = nullptr;
napi_value Js_GameEngineHelper::initJsEngineHelper(napi_env env, napi_callback_info info)
{
    _env = env;
    return 0;
}

void Js_GameEngineHelper::terminateProcess() {
    JSFunction::getFunction("ApplicationManager.exit").invoke<void>();
}

// The default accelerometer interval is 10000000 ns, that is, 10 ms.
float Js_GameEngineHelper::_accelerometerInterval = 10000000.0f;
bool Js_GameEngineHelper::_accelerometerFlag = false;
void Js_GameEngineHelper::enableAccelerometer() {
    // Start accelerometer subscription when allowed use default interval
    JSFunction::getFunction("Accelerometer.enable").invoke<void>(_accelerometerInterval);
    _accelerometerFlag = true;
}
    
void Js_GameEngineHelper::disableAccelerometer() {
    JSFunction::getFunction("Accelerometer.disable").invoke<void>();
    _accelerometerFlag = false;
}

void Js_GameEngineHelper::setAccelerometerInterval(float interval) {
    OHOS_LOGD("accelerometer setAccelerometerInterval, change to %{public}f", interval);
    // Same as the original one. No handling is required.
    if(_accelerometerInterval == interval) {
        return;
    }
    _accelerometerInterval = interval;

    // if accelerometer running, restart with new interval
    if(_accelerometerFlag) {
        JSFunction::getFunction("Accelerometer.enable").invoke<void>(_accelerometerInterval);
    }
}

int Js_GameEngineHelper::getCurrentTimeStamp() {
    return JSFunction::getFunction("DataTime.getTimeStamp").invoke<int>();
}


int Js_GameEngineHelper::getCurrentActiveTimeStamp(){
    return JSFunction::getFunction("DataTime.getActiveTimeStamp").invoke<int>();
}

int Js_GameEngineHelper::getScreenDpi(){
    return JSFunction::getFunction("DeviceUtils.getDpi").invoke<int>();
}

int Js_GameEngineHelper::getDisplayOrientation(){
    return JSFunction::getFunction("DeviceUtils.getDisplayOrientation").invoke<int>();
}